/**
 * @title HHIndexAction.java 
 * @description 	网站用户主页
 * @create 			2012-8-5 下午4:43:57 By xjiujiu
 * @package 		apps.site.action
 * @copyright 		Copyright (c) 2011-2012 http://xyrj.hhtc.edu.cn.com. All right reserved
 * @version         $Id$
 * XYRJ-Java-Project
 */

package apps.site.action;

import java.security.Security;
import java.util.Iterator;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Provider;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import system.config.HHConfig;
import system.pojo.HHMessagePojo;

import apps.admin.form.HHMessageForm;
import apps.site.model.HHMessageModel;

import com.xyrj.exception.HHValidateException;
import com.xyrj.mvc.HHAction;
import com.xyrj.utils.HHDatetime;

/**
 * 前台新闻显示
 * 
 * @ClassName: HHNewsAction
 * @Description:
 * @author maoyun0903(maoyun19900818@163.com);
 * @date 2012-8-22 上午10:02:11
 * @version V1.0
 */
public class HHMessageAction extends HHAction {

	/**
	 * 构造函数
	 * 
	 * 初始化类成员
	 * 
	 * @param request
	 * @param response
	 */
	public HHMessageAction(HttpServletRequest request,
			HttpServletResponse response) {
		super(request, response);

		this._pojo = new HHMessagePojo();
	}

	/**
	 * 首页方法
	 * 
	 * @description
	 * 
	 * @title index
	 * @return void
	 */
	public void index() {
		this._view.setRequestAttr("seoKeywords", HHConfig.SEO_KEYWORDS);
		this._view.setRequestAttr("seoDesc", HHConfig.SEO_DESC);

		this._view.theme("message_list.jsp");
	}

	/**
	 * 留言方法
	 * 
	 * @description
	 * 
	 * @title detail
	 * @return void
	 */
	public void leave_message() {
		try {
			this._assginPojoByAdd();
			this._pojo.setField("sort_num", "1");
			this._pojo.setField("pass_flag", "1");
			this._pojo.setField("top_flag", "1");
			this._pojo.setField("total_visits", "1");
			this._pojo.setField("edit_time", HHDatetime.getNow());
			this._pojo.setField("create_time", HHDatetime.getNow());
			HHMessageForm addMessageForm = new HHMessageForm();
			addMessageForm.validate(this._pojo);
			HHMessageModel messageModel = new HHMessageModel(this._pojo);
			if (0 < messageModel.add(this._pojo)) {
				this._view.alertAndGoBack("留言成功,请等待管理员审核~");
			} else {
				this._view.alertAndGoBack("留言失败,请联系管理员~");
			}
		} catch (HHValidateException e) {
			this._view.alertAndGoBack(e.getMessage());
		}
	}

	/**
	 * 测试发送Email方法
	 * 
	 * @Title: send_email
	 * @Description:
	 * @param name
	 *            email title context
	 * @return void
	 * @throws
	 */
	@SuppressWarnings("restriction")
	public void send_email() {
		String name = this._view.getParameter("name");
		String email = this._view.getParameter("email");
		String title = this._view.getParameter("title");
		String context = this._view.getParameter("context");

		String from = "学院软件用户  <" + email + ">";
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 
		this._sendSSLMessage(new String[] { "maoyun19900818@163.com" }, title,
				context, from);

	}

	/**
	 * 赋值模块的Pojo对象
	 * 
	 * @description
	 * 
	 * @title _assginPojo
	 * @return void
	 */
	@SuppressWarnings("rawtypes")
	protected void _assginPojoByAdd() {
		Iterator fields = this._pojo.getFields();
		while (fields.hasNext()) {
			String field = (String) (fields.next());
			if (false == this._pojo.isFileField(field)
					&& null != this._view.getParameter(field)
					&& !this._view.getParameter(field).equals("")) {
				this._pojo.setField(field, this._view.getParameter(field));
			}
		}
	}

	/**
	 * 发送邮件核心方法
	 * 
	 * @Title: sendSSLMessage
	 * @Description:
	 * @param @param recipients	发送到对方邮箱地址
	 * @param @param subject	主题
	 * @param @param message	邮件内容
	 * @param @param from	邮件来自于
	 * @param @throws MessagingException
	 * @return void
	 * @throws
	 */
	protected void _sendSSLMessage(String recipients[], String subject,
			String message, String from) {
		boolean debug = true;
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.auth", "true");
		props.put("mail.debug", "true");
		props.put("mail.smtp.port", 465);
		props.put("mail.smtp.socketFactory.port", 465);
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");

		Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(
								"maoyun19900818@gmail.com", "yy901991");
					}
				});

		session.setDebug(debug);
		try{
		Message msg = new MimeMessage(session);
		InternetAddress addressFrom = new InternetAddress(from);
		msg.setFrom(addressFrom);

		InternetAddress[] addressTo = new InternetAddress[recipients.length];
		for (int i = 0; i < recipients.length; i++) {
			addressTo[i] = new InternetAddress(recipients[i]);
		}
		msg.setRecipients(Message.RecipientType.TO, addressTo);

		msg.setSubject(subject);
		msg.setContent(message, "text/plain");
		Transport.send(msg);
		}catch(MessagingException ex){
			System.out.println(ex.getMessage());
		}
	}
}
